package com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.k_line_chart_portrait_content

import android.os.Bundle
import android.util.Log
import android.view.View
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseDataBindingFragment
import com.example.library_common.base.LazyFragment
import com.example.library_common.networking.RestClient
import com.example.library_common.utils.even.MessageWrap
import com.example.shushiyunsst.R
import com.example.shushiyunsst.databinding.KLineChartPortraitContentShortFragmentBinding
import com.example.shushiyunsst.fragment.main.transaction.child.TRANSFER_INDEX_DATA_REFRESH
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.KlineChartInterface
import com.example.shushiyunsst.fragment.main.transaction.child.kline_chart.bean.KlineChartShortBean

//简介
class KlineChartPortraitContentShortFragment :
    BaseDataBindingFragment<KLineChartPortraitContentShortFragmentBinding>(),
    KlineChartPortraitLabel {

    companion object {
        fun newInstance(): KlineChartPortraitContentShortFragment {
            val args = Bundle()
            val fragment = KlineChartPortraitContentShortFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var onRefreshing = false

    private var transactionUIInvokeListener: KlineChartInterface? = null

    private var mShortBean: KlineChartShortBean? = null

    override fun setTransactionUIInvokeListener(listener: KlineChartInterface?) {
        this.transactionUIInvokeListener = listener

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewBinder.bean = mShortBean
        val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
        val tokenName = transIndexNode?.iconType?.tokenName ?: ""
        viewBinder.tokenType = tokenName
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    override fun getTitle(): String {
        return "简介"
    }


    override fun handMessage(msg: MessageWrap?) {
        super.handMessage(msg)
        when (msg?.message) {
            TRANSFER_INDEX_DATA_REFRESH -> {
                netRequest()
            }
        }
    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        if (mShortBean == null) {
            netRequest()
        }
    }

    private fun netRequest() {

        if (onRefreshing && isSupportVisible) return
        onRefreshing = true

        val transIndexNode = transactionUIInvokeListener?.getTransIndexNode()
        val tokenName = transIndexNode?.iconType?.tokenName ?: ""
        val map = mapOf(
            "tokenType" to tokenName
        )
        RestClient.builder()
            .raw(JSON.toJSONString(map))
            .url(R.string.queryTokenDetail)
            .success {
                onRefreshing = false
                parseQueryTokenDetail(it)
            }.error { _, _ ->
                onRefreshing = false
            }.failure {
                onRefreshing = false
            }
            .build().post()
    }


    private fun parseQueryTokenDetail(it: String?) {
        mShortBean = JSON.parseObject(it, KlineChartShortBean::class.java)
        viewBinder.bean = mShortBean

    }


    override fun layoutId(): Int {
        return R.layout.k_line_chart_portrait_content_short_fragment
    }


}